package com.yanqu.road.dao.impl.activity.dunhuangshiku;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuEventConfig;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuMuralConfig;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuRegionConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/21 12:55
 */
public class DunHuangShiKuActivityConfigDaoImpl extends TempDao {

    public static String getDunHuangShiKuMuralConfigSql = "select * from t_s_activity_dunhuang_mural where activityId = ? order by id asc";
    public List<DunHuangShiKuMuralConfig> getDunHuangShiKuMuralConfigList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DunHuangShiKuMuralConfig> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDunHuangShiKuMuralConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DunHuangShiKuMuralConfig dunHuangShiKuMuralConfig = new DunHuangShiKuMuralConfig();
                dunHuangShiKuMuralConfig.setActivityId(rs.getInt("activityId"));
                dunHuangShiKuMuralConfig.setId(rs.getInt("id"));
                dunHuangShiKuMuralConfig.setIcon(rs.getString("icon"));
                dunHuangShiKuMuralConfig.setStar(rs.getInt("star"));
                dunHuangShiKuMuralConfig.setColour(rs.getString("colour"));
                dunHuangShiKuMuralConfig.setReward(rs.getString("reward"));
                list.add(dunHuangShiKuMuralConfig);
                // 解析
                String[] str = dunHuangShiKuMuralConfig.getColour().split("\\|");
                for(String item : str){
                    dunHuangShiKuMuralConfig.needColourList.add(Integer.parseInt(item));
                }
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String getDunHuangShiKuRegionConfigSql = "select * from t_s_activity_dunhuang_region where activityId = ? order by id asc";
    public List<DunHuangShiKuRegionConfig> getDunHuangShiKuRegionConfigList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DunHuangShiKuRegionConfig> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDunHuangShiKuRegionConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DunHuangShiKuRegionConfig dunHuangShiKuRegionConfig = new DunHuangShiKuRegionConfig();
                dunHuangShiKuRegionConfig.setActivityId(rs.getInt("activityId"));
                dunHuangShiKuRegionConfig.setId(rs.getInt("id"));
                dunHuangShiKuRegionConfig.setEnergy(rs.getInt("energy"));
                dunHuangShiKuRegionConfig.setConsumeCondition(rs.getString("consumeCondition"));
                dunHuangShiKuRegionConfig.setEventCondition(rs.getString("eventCondition"));
                dunHuangShiKuRegionConfig.setEventWeight(rs.getString("eventWeight"));
                list.add(dunHuangShiKuRegionConfig);
                // 解析
                if(!StringUtils.isNullOrEmpty(dunHuangShiKuRegionConfig.getEventCondition()) && !"0".equals(dunHuangShiKuRegionConfig.getEventCondition())){
                    String[] str = dunHuangShiKuRegionConfig.getEventCondition().split("-");
                    dunHuangShiKuRegionConfig.eventNum = Integer.valueOf(str[0]);
                    String[] eventStr = str[1].split("\\|");
                    for(String eventId : eventStr){
                        dunHuangShiKuRegionConfig.eventList.add(Integer.valueOf(eventId));
                    }
                }
                if(!StringUtils.isNullOrEmpty(dunHuangShiKuRegionConfig.getEventWeight()) && !"0".equals(dunHuangShiKuRegionConfig.getEventWeight())) {
                    String[] eventWeightStr = dunHuangShiKuRegionConfig.getEventWeight().split("\\|");
                    for (String item : eventWeightStr) {
                        String[] eventWeight = item.split(";");
                        dunHuangShiKuRegionConfig.eventMap.put(Integer.valueOf(eventWeight[0]), Integer.valueOf(eventWeight[1]));
                    }
                }
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String getDunHuangShiKuEventConfigSql = "select * from t_s_activity_dunhuang_event where activityId = ? order by id asc";
    public List<DunHuangShiKuEventConfig> getDunHuangShiKuEventConfigList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DunHuangShiKuEventConfig> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDunHuangShiKuEventConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DunHuangShiKuEventConfig dunHuangShiKuEventConfig = new DunHuangShiKuEventConfig();
                dunHuangShiKuEventConfig.setActivityId(rs.getInt("activityId"));
                dunHuangShiKuEventConfig.setId(rs.getInt("id"));
                dunHuangShiKuEventConfig.setEventType(rs.getInt("eventType"));
                dunHuangShiKuEventConfig.setTargetId(rs.getInt("targetId"));
                dunHuangShiKuEventConfig.setBookNum(rs.getString("bookNum"));
                dunHuangShiKuEventConfig.setScore(rs.getInt("score"));
                dunHuangShiKuEventConfig.setReward(rs.getString("reward"));
                dunHuangShiKuEventConfig.setSecurityTimes(rs.getInt("securityTimes"));
                dunHuangShiKuEventConfig.setParam(rs.getString("param"));
                dunHuangShiKuEventConfig.setIsShowLog(rs.getInt("isShowLog"));
                list.add(dunHuangShiKuEventConfig);
                // 解析
                if(!StringUtils.isNullOrEmpty(dunHuangShiKuEventConfig.getBookNum()) && !"0".equals(dunHuangShiKuEventConfig.getBookNum())) {
                    String[] str = dunHuangShiKuEventConfig.getBookNum().split(";");
                    dunHuangShiKuEventConfig.num = Integer.valueOf(str[0]);
                    dunHuangShiKuEventConfig.probability = Integer.valueOf(str[1]);
                }
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
